//增加新卡
var newCardId;
function savePlan(){
	$("#form_apply_card").form({
		success: function(v){
			var result=v.match(/^\"p\d+\"$/);
			if(result){
				newCardId=v.substring(1,v.length-1);
				console.log(newCardId);
				$("#charge_apply_card").panel('refresh');
				$("#charge_apply_card").panel('open');
			}else{
				$.messager.alert("办理提示",v.substring(1,v.length-1));
			}
		}
	});
	$("#form_apply_card").submit();
}

//点击办卡缴费
function charge_apply_card(){
	$("#charge_apply_card_form").form({
		url:'applyCard',
		success: function(v){
			$.messager.alert("操作提示",v.substring(1,v.length-1));
			$("#charge_apply_card").panel("close",true);
		}
	});
	$("#charge_apply_card_form").submit();
}

//验证规则
$.extend($.fn.validatebox.defaults.rules, {    
    people_name: {    
        validator: function(value, param){  
        	var reg = /^[\u4e00-\u9fa5]{2,10}$/;
            return reg.test(value);    
        },    
        message: '请输入2到10位的中文'   
    },  
    age:{
    	validator: function(value, param){  
        	var reg = /^[1-9]\d{0,2}$/;
            return reg.test(value);    
        },    
        message: '年龄第一位不能为零，不能超过三位数' 
    },
    id_card:{
    	validator: function(value, param){  
        	var reg1 = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;//18位身份证
        	var reg2 = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$/;//15位身份证
            return reg1.test(value)||reg2.test(value);    
        },    
        message: '请输入正确的身份证' 
    },
    address:{
    	validator: function(value, param){  
        	var reg = /^[\u4e00-\u9fa5]+$/;
            return reg.test(value);    
        },    
        message: '只能输入中文哦' 
    },
    card_id:{
    	validator: function(value, param){  
        	var reg = /^[a-z0-9]+$/;
            return reg.test(value);    
        },    
        message: '只能输入英文和数字哦' 
    },
    search_name:{
    	validator: function(value, param){  
        	var reg = /^[\u4e00-\u9fa5]{1,10}$/;
            return reg.test(value);    
        },    
        message: '请输入1到10位的中文' 
    }
}); 

//加载操作栏按钮
var card_caozuo=function black_white_list_buttom(value,row,index){
	var btm="";
	if(row.status=="正常"){
		btm="<input type='button' class='normal' onclick='set_card_lost(\""+row.cradId+"\")' value='挂失'/>";
	}
	var btm2="<input type='button' class='normal eidt' onclick='edit_card("+index+")' value='编辑'/>";
	return "<center>"+btm+btm2+"</center>";
};
//挂失诊疗卡
function set_card_lost(card_id){
	$.messager.confirm("提示","是否挂失此卡："+card_id,function(r){
		if(r){
			$("<form method='post'>").form({
				url: 'set_card_lost',
				onSubmit:function(param){
					param.card_id=card_id;
				},
				success: function(v){
					$.messager.alert("办理结果",v.substring(1,v.length-1));
					$("#check_all_card").datagrid("reload");
				}
			}).submit();
		}
	});	
};
var card_id;
var name;
var age;
var sex;
var idCard;
var address;
//点击编辑按钮
function edit_card(index){
	$("#check_all_card").datagrid('selectRow',index);
	var row2=$("#check_all_card").datagrid('getSelected');
	card_id=row2.cradId;
	name=row2.name;
	age=row2.age;
	sex=row2.sex;
	idCard=row2.idCard;
	address=row2.address;
	$("#load_card_detail").panel('refresh');
	$("#load_card_detail").panel('open');
};

//给诊疗卡详情子界面赋值
function fuzhi(){
	$("#card_detail_card_id").textbox('setValue',card_id);
	$("#card_detail_name").textbox("setValue",name);
	$("#card_detail_age").textbox("setValue",age);
	$("#card_detail_sex").combobox("setText",sex);
	$("#card_detail_idCard").textbox("setValue",idCard);
	$("#card_detail_address").textbox("setValue",address);
}
//保存修改的诊疗卡
function save_change_card_detail(){
	$("#save_change_card_detail").form({
		url:'save_change_card_detail',
		success: function(v){
			$.messager.alert("操作提示",v.substring(1,v.length-1));
			$("#load_card_detail").panel("close",true);
			$("#check_all_card").datagrid("reload");
		}
	});
	$("#save_change_card_detail").submit();
} 
//查看所有诊疗卡
function check_all_card(){
	$("#check_all_card").datagrid("load",{page:'1'});
}
//时间段搜索
function check_by_time(){
	var start_time=$("#card_time_start").combo('getText');
	var end_time=$("#card_time_end").combo('getText');
	var param="{page:'1'";
	var bool=false;
	if(start_time!=""){
		param+=",begin_time:'"+start_time+"'";
		bool=true;
	}if(end_time!=""){
		param+=",end_time:'"+end_time+"'";
		bool=true;
	}if(!bool){
		$.messager.alert("提示","请选择时间");
		return '';
	}
	param+="}";
	var param2= eval('(' + param + ')'); 
	$("#check_all_card").datagrid("load",param2);
}
//混合查询
function check_by_all_condition(){
	var start_time=$("#card_time_start").combo('getText');
	var end_time=$("#card_time_end").combo('getText');
	var condition=$("#card_searchbox").searchbox("getName");
	var status=$("#card_status").combo("getText");
	var value=$("#card_searchbox").searchbox("getValue");
	var sex=$("#card_sex").combo("getText");
	var param="{page:'1'";
	if(start_time!=""){
		param+=",begin_time:'"+start_time+"'";
	}if(end_time!=""){
		param+=",end_time:'"+end_time+"'";
	}if(value!=""){
		param+=","+condition+":'"+value+"'";
	}if(status!="状态"){
		param+=",status:'"+status+"'";
	}if(sex!="性别"){
		param+=",sex:'"+sex+"'";
	}
	param+="}";
	console.log(param);
	var param2= eval('(' + param + ')'); 
	$("#check_all_card").datagrid("load",param2);
}

//按下回车键时
function load_card_by_condition(){
	if($("#card_searchbox").searchbox('isValid')){
		var condition=$("#card_searchbox").searchbox("getName");
		var value=$("#card_searchbox").searchbox("getValue");
		var parm="{"+condition+":'"+value+"'}";
		var param2= eval('(' + parm + ')'); 
		if(value!=""){
			$("#check_all_card").datagrid("load",param2);
		}else{
			$.messager.alert("搜索提示","请输入搜索条件");
		}
	}
}

//选择搜索条件时
var select_item="";
/*function on_select_menu(item){
	console.log("选择");
	select_item=item;
	if(item.name=="name"){
		var a=$("#card_name").attr("data-options")+",selected:true";
		$("#card_name").attr("data-options",a);
		$("#card_searchbox").searchbox('selectName',item.name,{
			validType:'search_name'
		})
	}else{
		var a=$("#card_name").attr("data-options").split(",");
		$("#card_name").attr("data-options",a[0]);
	}
	if(item.name=="age"){
		var a=$("#card_age").attr("data-options")+",selected:true";
		$("#card_age").attr("data-options",a);
		$("#card_searchbox").searchbox({
			validType:'age',
			menu:'#card_condition',
		})
	}else{
		var a=$("#card_age").attr("data-options").split(",");
		$("#card_age").attr("data-options",a[0]);
	}
	if(item.name=="cradId"){
		var a=$("#card_number").attr("data-options")+",selected:true";
		$("#card_number").attr("data-options",a);
		$("#card_searchbox").searchbox({
			validType:'card_id',
			menu:'#card_condition',
		})
	}else {
		var a=$("#card_number").attr("data-options").split(",");
		$("#card_number").attr("data-options",a[0]);
	}
	if(item.name=="idCard"){
		var a=$("#card_id_card").attr("data-options")+",selected:true";
		$("#card_id_card").attr("data-options",a);
		$("#card_searchbox").searchbox({
			validType:'id_card',
		})
	}else {
		var a=$("#card_id_card").attr("data-options").split(",");
		$("#card_id_card").attr("data-options",a[0]);
	}
	if(item.name=="address"){
		$("#card_searchbox").searchbox({
			validType:'address',
			
		})
	}
}*/
function on_select_menu(item){
	var select_;
	var valid;
	if(item.name=="name"){
		select_=$("#card_name");
		valid="search_name";
	}else if(item.name=="age"){
		select_=$("#card_age");
		valid="age";
	}else if(item.name=="cradId"){
		select_=$("#card_number");
		valid="card_id";
	}else if(item.name=="idCard"){
		select_=$("#card_id_card");
		valid="id_card";
	}else if(item.name=="address"){
		select_=$("#card_address");
		valid="address";
	}
	$("#card_condition").children().each(function(){
		if($(this)!=select_){
			var option=$(this).attr("name");
			$(this).attr("data-options","name:'"+option+"'");
		}
	});
	var a=select_.attr("data-options")+",selected:true";
	select_.attr("data-options",a);
	$("#card_searchbox").searchbox("selectName",item.name);
	$("#card_searchbox").searchbox({
		validType:valid,
	})
}


function set_selected(){
	console.log("隐藏");
	$("#card_searchbox").searchbox('selectName',select_item.name);
}
var buttons = $.extend([],$.fn.datebox.defaults.buttons);
buttons.splice(1,0,{
	text: '清空',
	handler: function(target){
	$(target).datebox('setValue',"");
}
});

